package com.geticsen.leetcode;

public class Solution_63 {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        obstacleGrid[0][0] = 1;
        for (int j = 1; j < m; j++) {
            if (obstacleGrid[0][j] == 0) {
                obstacleGrid[0][j] = obstacleGrid[0][j - 1];
            } else {
                break;
            }
        }
        for (int j = 1; j < n; j++) {
            if (obstacleGrid[j][0] == 0) {
                obstacleGrid[j][0] = obstacleGrid[j - 1][0];
            } else {
                break;
            }
        }
        for (int i = 1; i < n; i++) {
            for (int j = 1; j < m; j++) {
                if (obstacleGrid[i][j] == 1) continue;
                obstacleGrid[i][j] = (obstacleGrid[i][j - 1] == 1 && j - 1 != 0 ? 0 : obstacleGrid[i][j - 1])
                        + (obstacleGrid[i - 1][j] == 1 && i - 1 != 0 ? 0 : obstacleGrid[i - 1][j]);
            }
        }
        return obstacleGrid[n - 1][m - 1];
    }
}
